//
//  PolicyFileViewController.m
//  homeland
//
//  Created by SunShine on 13-10-30.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "PolicyFileViewController.h"

@interface PolicyFileViewController ()
@property (strong, nonatomic) PolicyFileService *dataService;
@end

@implementation PolicyFileViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"传入的ID为%@", _contentID);
    self.title = @"详情";
    self.dataService = [PolicyFileService new];
    _dataService.delegate = self;
    [_dataService getFileContentForID:_contentID];
    [self startLoadingView];
    [self configView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)configView
{
    if ([_dataService.dataList count] == 0) {
        _titleTextView.text = @"";
        _line.hidden = YES;
        _contentTextView.text = @"";
        return;
    }
    EnterprisePolicyFileContentItem *item = [_dataService.dataList objectAtIndex:0];
    _titleTextView.text = item.title;
    _line.hidden = NO;
    _contentTextView.text = item.content;
    //调整高度
    CGFloat heightTitle = [self caculateHeightForString:item.title AndFontSize:18.0 AndWitdh:320.0];
    CGFloat heightContent = [self caculateHeightForString:item.content AndFontSize:14.0 AndWitdh:320.0];
    _titleTextView.frame = CGRectMake(0.0, 0.0, 320.0, heightTitle);
    _line.frame = CGRectMake(0.0, heightTitle, 320.0, 2.0);
    _contentTextView.frame = CGRectMake(0.0, heightTitle+2.0, 320.0, heightContent);
    CGFloat heightTotal = MAX(CGRectGetMaxY(_scrollView.frame), CGRectGetMaxY(_contentTextView.frame));
    _scrollView.contentSize = CGSizeMake(320.0, heightTotal);
}

#pragma mark - Data Service
-(void)loadDataFinished
{
    [self stopLoadingView];
    [self configView];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
}
@end
